package com.jindy.module.stock.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.module.stock.domain.Stock;
import com.jindy.module.stock.dto.StockDto;
import com.jindy.module.stock.dto.StockVo;
import com.jindy.module.stock.service.IStockService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.core.page.TableDataInfo;

/**
 * 库存Controller
 *
 * @author ruoyi
 * @date 2022-10-08
 */
@RestController
@RequestMapping("/module/stock")
public class StockController extends BaseController
{
    @Autowired
    private IStockService stockService;

    /**
     * 查询库存列表
     */
    @PreAuthorize("@ss.hasPermi('module:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(Stock stock)
    {
        startPage();
        List<StockDto> list = stockService.selectStockDtoList(stock);
        return getDataTable(list);
    }

    /**
     * 查询库存列表（非模糊查询）
     */
//    @PreAuthorize("@ss.hasPermi('module:stock:list')")
    @GetMapping("/list2")
    public TableDataInfo list2(Stock stock)
    {
        startPage();
        List<Stock> list = stockService.selectStockList(stock);
        return getDataTable(list);
    }

    /**
     * 查询库存列表及每种型号价格（非模糊查询）
     */
    //    @PreAuthorize("@ss.hasPermi('module:stock:list')")
    @GetMapping("/list3")
    public TableDataInfo list3(Stock stock)
    {
        startPage();
        List<StockVo> list = stockService.selectStockList3(stock);
        return getDataTable(list);
    }


    /**
     * 查询库存列表（根据仓库名排序）
     */
    @PreAuthorize("@ss.hasPermi('module:stock:listByWarehouse')")
    @GetMapping("/listByWarehouse")
    public TableDataInfo list4(Stock stock)
    {
        startPage();
        List<StockDto> list = stockService.selectStockDtoList2(stock);
        return getDataTable(list);
    }

    /**
     * 导出库存列表
     */
    @Log(title = "库存", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('module:stock:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Stock stock)
    {
        List<Stock> list = stockService.selectStockList(stock);
        ExcelUtil<Stock> util = new ExcelUtil<Stock>(Stock.class);
        util.exportExcel(response, list, "库存数据");
    }

    /**
     * 获取库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:stock:id')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(stockService.selectStockById(id));
    }

    /**
     * 新增库存
     */
    @Log(title = "库存", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('module:stock:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Stock stock)
    {
        return toAjax(stockService.insertStock(stock));
    }

    /**
     * 修改库存
     */
    @Log(title = "库存", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('module:stock:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Stock stock)
    {
        return toAjax(stockService.updateStock(stock));
    }

    /**
     * 删除库存
     */
    @Log(title = "库存", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('module:stock:remove')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockService.deleteStockByIds(ids));
    }

    /**
     * 查询
     */
    @Log(title = "库存", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('module:stock:getStock')")
    @PostMapping("/getStock")
    public AjaxResult getStock(@RequestBody StockDto stock)
    {
        return AjaxResult.success(stockService.getStock(stock));
    }

}
